import {
  Body,
  Controller,
  Delete,
  Get,
  HttpCode,
  HttpStatus,
  NotFoundException,
  Param,
  Post,
  Put,
  ValidationPipe,
} from '@nestjs/common';
import { ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger';
import { Room } from '../entities/room.entity';
import { CreateRoomDto, UpdateRoomDto } from './dto';
import { RoomsService } from './rooms.service';

@ApiTags('rooms')
@Controller('rooms')
export class RoomsController {
  constructor(private readonly roomsService: RoomsService) {}

  @Get()
  @ApiOperation({ summary: '获取所有房间', description: '返回所有房间的列表' })
  @ApiResponse({ status: 200, description: '成功获取房间列表', type: [Room] })
  async findAll(): Promise<Room[]> {
    return this.roomsService.findAll();
  }

  @Get(':id')
  @ApiOperation({ summary: '获取指定房间', description: '根据ID获取房间详情' })
  @ApiParam({ name: 'id', description: '房间ID' })
  @ApiResponse({ status: 200, description: '成功获取房间详情', type: Room })
  @ApiResponse({ status: 404, description: '房间不存在' })
  async findOne(@Param('id') id: string): Promise<Room> {
    const roomId = parseInt(id, 10);
    if (isNaN(roomId)) {
      throw new NotFoundException('房间ID必须是数字');
    }
    return this.roomsService.findOne(roomId);
  }

  @Get('hotel/:hotelId')
  @ApiOperation({
    summary: '获取酒店的所有房间',
    description: '根据酒店ID获取其所有房间',
  })
  @ApiParam({ name: 'hotelId', description: '酒店ID' })
  @ApiResponse({ status: 200, description: '成功获取房间列表', type: [Room] })
  @ApiResponse({ status: 404, description: '酒店不存在' })
  async findByHotel(@Param('hotelId') hotelId: string): Promise<Room[]> {
    const id = parseInt(hotelId, 10);
    if (isNaN(id)) {
      throw new NotFoundException('酒店ID必须是数字');
    }
    return this.roomsService.findByHotel(id);
  }

  @Post()
  @ApiOperation({ summary: '创建房间', description: '创建新的房间' })
  @ApiResponse({ status: 201, description: '成功创建房间', type: Room })
  async create(
    @Body(ValidationPipe) createRoomDto: CreateRoomDto,
  ): Promise<Room> {
    return this.roomsService.create(createRoomDto);
  }

  @Put(':id')
  @ApiOperation({ summary: '更新房间', description: '更新指定ID的房间信息' })
  @ApiParam({ name: 'id', description: '房间ID' })
  @ApiResponse({ status: 200, description: '成功更新房间', type: Room })
  @ApiResponse({ status: 404, description: '房间不存在' })
  async update(
    @Param('id') id: string,
    @Body(ValidationPipe) updateRoomDto: UpdateRoomDto,
  ): Promise<Room> {
    const roomId = parseInt(id, 10);
    if (isNaN(roomId)) {
      throw new NotFoundException('房间ID必须是数字');
    }
    return this.roomsService.update(roomId, updateRoomDto);
  }

  @Delete(':id')
  @ApiOperation({ summary: '删除房间', description: '删除指定ID的房间' })
  @ApiParam({ name: 'id', description: '房间ID' })
  @ApiResponse({ status: 204, description: '成功删除房间' })
  @ApiResponse({ status: 404, description: '房间不存在' })
  @HttpCode(HttpStatus.NO_CONTENT)
  async remove(@Param('id') id: string): Promise<void> {
    const roomId = parseInt(id, 10);
    if (isNaN(roomId)) {
      throw new NotFoundException('房间ID必须是数字');
    }
    return this.roomsService.remove(roomId);
  }
}
